﻿using System;
using System.Net;
using System.Linq;
using MEFedMVVM.ViewModelLocator;
using System.ComponentModel.Composition;
using Cinch;

using Microsoft.Maps.MapControl;
using System.Collections.ObjectModel;
using System.Collections;
using System.Xml;
using System.IO;
using WebProject.Controls;
using System.ComponentModel;
using System.Windows;
using WebProject.ServiceReferenceProduct;

namespace WebProject.ViewModels
{
    [ExportViewModel("ProductsPageViewModel")]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class ProductsPageViewModel : ViewModelBase
    {
        //Constructor
        public ProductsPageViewModel()
        {
            ProductsPageText = "ProductsPageViewModel";
            ButtonRentClickCommand = new DelegateCommand(RentClickAction);

            Load();
        }
        //PropertyChanged Event
        public new event PropertyChangedEventHandler PropertyChanged;
        public string ProductsPageText { get; set; }
        //ProductList , Binding XAML
        public ObservableCollection<ListData> _ProductData = new ObservableCollection<ListData>();
        public ObservableCollection<ListData> ProductData
        {
            get { return this._ProductData; }
            set
            {
                if (this._ProductData == value) return;
                this._ProductData = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("ProductData"));
            }
        }
        //OnPropertyChanged, Called when [property changed].
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, e);
        }
        //Button, Binding XAML
        public DelegateCommand ButtonRentClickCommand { get; set; }
        //Event Rent a product
        public void RentClickAction(object sender)
        {
            var allRent = this.ProductData.Where(c => c.Checked).Select(c => c.ID);
            var txt = "Rent a Bike:";
            if (allRent != null)
                foreach (var r in allRent)
                    txt += " " + r;

            MessageBox.Show(txt);
        }
        //LoadData
        public void Load()
        {
            var proxy = new ProductServiceClient();
            proxy.ProductDataListCompleted += ProxyProductDataListCompleted;
            proxy.ProductDataListAsync();

            #region Without Service
            //_ProductData.Add(new ListData { Checked = false, Name = "Thing One", ID = 1, Desc = "aaaaa", ImageName = "http://mammut09.files.wordpress.com/2010/04/fahrrad-fahren-6310421.jpeg" });
            //_ProductData.Add(new ListData { Checked = false, Name = "Thing Two", ID = 2, Desc = "bbbbb", ImageName = "http://mammut09.files.wordpress.com/2010/04/fahrrad-fahren-6310421.jpeg" });
            //_ProductData.Add(new ListData { Checked = false, Name = "Thing Three", ID = 3, Desc = "ccccc", ImageName = "http://mammut09.files.wordpress.com/2010/04/fahrrad-fahren-6310421.jpeg" });

            #endregion
        }
        //Calling web service, if data are complete 
        private void ProxyProductDataListCompleted(object sender, ProductDataListCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                var id = 1;
                foreach (var item in e.Result)
                {
                    _ProductData.Add(new ListData
                    {
                        Checked = false,
                        Desc = item.Desc,
                        ID = id,
                        ImageName = item.ImageName,
                        Name = item.Name
                    });
                    id++;
                }
            }


        }
    }

    //ListData for the products
    public class ListData
    {
        public string Name { get; set; }
        public int ID { get; set; }
        public string Desc { get; set; }
        public string ImageName { get; set; }
        public bool Checked { get; set; }
    }
}
